package com.raymond.smgp.codec.decoding;

import cn.hutool.core.util.HexUtil;
import com.raymond.client.codec.decoding.BaseDecoder;
import com.raymond.client.msg.BaseAgreement;
import com.raymond.client.utils.NettyByteBufUtil;
import com.raymond.smgp.constants.SmgpConstants;
import com.raymond.smgp.msg.request.DeliverRequestMessage;
import com.raymond.smgp.msg.request.ReportRequestMessage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;

import java.util.List;

/**
 * 状态报告和上行解码
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-08-06 10:48
 */
public class DeliverRequestMessageDecoder extends BaseDecoder {
    @Override
    public void decode(ChannelHandlerContext ctx, BaseAgreement msg, List<Object> out) throws Exception {
        DeliverRequestMessage requestMessage = new DeliverRequestMessage(msg.getSequenceId());
        ByteBuf bodyBuffer = Unpooled.wrappedBuffer(msg.getBodyBuffer());
        requestMessage.setMsgId(HexUtil.encodeHexStr(NettyByteBufUtil.toArray(bodyBuffer, 10)));
        boolean isReport = bodyBuffer.readByte() == (byte) 1;
        requestMessage.setReport(isReport);
        requestMessage.setMsgFmt((byte) bodyBuffer.readUnsignedByte());
        requestMessage.setRecvTime(bodyBuffer.readCharSequence(14, SmgpConstants.DEFAULT_TRANSPORT_CHARSET).toString().trim());
        requestMessage.setSrcTermId(bodyBuffer.readCharSequence(21, SmgpConstants.DEFAULT_TRANSPORT_CHARSET).toString().trim());
        requestMessage.setDestTermId(bodyBuffer.readCharSequence(21, SmgpConstants.DEFAULT_TRANSPORT_CHARSET).toString().trim());
        short msgLength = (short) (bodyBuffer.readUnsignedByte() & 0xffff);
        requestMessage.setMsgLength(msgLength);
        if (isReport && msgLength > 0) {
            ReportRequestMessage report = new ReportRequestMessage(msg.getSequenceId());
            bodyBuffer.readCharSequence("id:".length(), SmgpConstants.DEFAULT_TRANSPORT_CHARSET);
            report.setMsgId(HexUtil.encodeHexStr(NettyByteBufUtil.toArray(bodyBuffer, 10)));
            bodyBuffer.readCharSequence(" sub:".length(), SmgpConstants.DEFAULT_TRANSPORT_CHARSET);
            report.setSub(bodyBuffer.readCharSequence(3, SmgpConstants.DEFAULT_TRANSPORT_CHARSET).toString().trim());
            bodyBuffer.readCharSequence(" dlvrd:".length(), SmgpConstants.DEFAULT_TRANSPORT_CHARSET);
            report.setDlvrd(bodyBuffer.readCharSequence(3, SmgpConstants.DEFAULT_TRANSPORT_CHARSET).toString().trim());
            bodyBuffer.readCharSequence(" submit date:".length(), SmgpConstants.DEFAULT_TRANSPORT_CHARSET);
            report.setSubmitTime(bodyBuffer.readCharSequence(10, SmgpConstants.DEFAULT_TRANSPORT_CHARSET).toString().trim());
            bodyBuffer.readCharSequence(" done date:".length(), SmgpConstants.DEFAULT_TRANSPORT_CHARSET);
            report.setDoneTime(bodyBuffer.readCharSequence(10, SmgpConstants.DEFAULT_TRANSPORT_CHARSET).toString().trim());
            bodyBuffer.readCharSequence(" stat:".length(), SmgpConstants.DEFAULT_TRANSPORT_CHARSET);
            report.setStat(bodyBuffer.readCharSequence(7, SmgpConstants.DEFAULT_TRANSPORT_CHARSET).toString().trim());
            bodyBuffer.readCharSequence(" err:".length(), SmgpConstants.DEFAULT_TRANSPORT_CHARSET);
            report.setErr(bodyBuffer.readCharSequence(3, SmgpConstants.DEFAULT_TRANSPORT_CHARSET).toString().trim());
            //读取剩余的长度，标准是20个字节，福建电信是3个字节，
            bodyBuffer.readCharSequence(" text:".length(), SmgpConstants.DEFAULT_TRANSPORT_CHARSET);
            report.setText(bodyBuffer.readCharSequence(msgLength - 102, SmgpConstants.DEFAULT_TRANSPORT_CHARSET).toString().trim());

            requestMessage.setReportRequestMessage(report);
        } else {//mo
            byte[] contentBytes = new byte[msgLength];
            bodyBuffer.readBytes(contentBytes);
            requestMessage.setMsgContent(contentBytes);
        }
        requestMessage.setReserve(bodyBuffer.readCharSequence(8, SmgpConstants.DEFAULT_TRANSPORT_CHARSET).toString().trim());

        //TLV 部分
        requestMessage.decodeTLV(bodyBuffer);
        out.add(requestMessage);
    }
}
